/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.voto.sunflower.fragment;

import android.app.AlertDialog.Builder;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easemob.chat.EMChatOptions;
import com.voto.sunflower.R;
import com.voto.sunflower.SunflowerApplication;
import com.voto.sunflower.activity.me.MyQRCodeActivity;
import com.voto.sunflower.activity.me.PersonalSettingActivity;
import com.voto.sunflower.activity.me.SettingActivity;
import com.voto.sunflower.dao.User;
import com.voto.sunflower.utils.ExUtils;
import com.voto.sunflower.utils.URLHelper;
import com.voto.sunflower.widget.BottomDialog;
import com.voto.sunflower.widget.BottomDialog.OnClickDialogListener;

/**
 * 设置界面
 * 
 * @author Administrator
 * 
 */
public class SettingsFragment extends Fragment implements OnClickListener,
		android.content.DialogInterface.OnClickListener {
	private String AVATAR_TEMP_PATH = SunflowerApplication.AVATAR_TEMP_PATH
			+SunflowerApplication.getmUser().getId()
			+SunflowerApplication.AVATAR_NAME;
	private static final String TAG = "SettingFragment";
	private EMChatOptions chatOptions;
	private RelativeLayout parents_info_area;
	private RelativeLayout my_two_dimension_code;
	private RelativeLayout setting;
	private RelativeLayout contack_service;
	private RelativeLayout share_friends;
	private ListView mListView;
	private View layoutView;
	private Builder serviceDialog;
	private ImageView avatarImageView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "settingfragment onCreateview---------->");
		layoutView = inflater.inflate(R.layout.fragment_me, container, false);

		initView(layoutView);
		return layoutView;
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "settingfragment onResume---------->");
		setAvatarInfo();
	}

	// 设置头像信息
	public void setAvatarInfo() {
		// 设置头像栏信息
		TextView nameTextView = (TextView) layoutView
				.findViewById(R.id.text_name);
		TextView idTextView = (TextView) layoutView.findViewById(R.id.text_id);
		avatarImageView = (ImageView) layoutView
				.findViewById(R.id.image_avatar);

		User user = SunflowerApplication.getmUser();
		String name = user.getName();
		String id = user.getVisible_id();
		String avatar_uri = user.getAvatar_url();

		setMyAvatar(avatar_uri);
		nameTextView.setText(name);
		idTextView.setText("ID:" + id);
	}

	private void setMyAvatar(String avatar_uri) {
		// 获取头像，有就缓存
		Bitmap bitmap = BitmapFactory
				.decodeFile(AVATAR_TEMP_PATH);
		if (bitmap != null) {
			avatarImageView.setImageBitmap(bitmap);
		} else {
			ExUtils.loadImage(URLHelper.getAbsoluteUrl(avatar_uri),
					R.drawable.unkown_person, avatarImageView);
		}
	}

	// 初始化标题栏
	private void initView(View rootView) {
		TextView actionBarTitle = (TextView) rootView.findViewById(R.id.title);
		TextView actionBarBack = (TextView) rootView.findViewById(R.id.back);
		actionBarTitle.setText(getString(R.string.me_main_title));
		actionBarBack.setVisibility(View.INVISIBLE);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		parents_info_area = (RelativeLayout) getView().findViewById(
				R.id.parents_info_area);
		my_two_dimension_code = (RelativeLayout) getView().findViewById(
				R.id.my_two_dimension_code);
		share_friends = (RelativeLayout) getView().findViewById(
				R.id.share_friends);
		contack_service = (RelativeLayout) getView().findViewById(
				R.id.contack_service);
		setting = (RelativeLayout) getView().findViewById(R.id.setting);

		parents_info_area.setOnClickListener(this);
		my_two_dimension_code.setOnClickListener(this);
		share_friends.setOnClickListener(this);
		contack_service.setOnClickListener(this);
		setting.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.parents_info_area:
			startActivity(new Intent(getActivity(),
					PersonalSettingActivity.class));
			break;

		case R.id.my_two_dimension_code:
			startActivity(new Intent(getActivity(), MyQRCodeActivity.class));
			break;
		case R.id.share_friends:
			BottomDialog dialog = new BottomDialog(getActivity(),
					R.style.MyDialogBottomNormal);
			dialog.setmTitle("修改个人头像");
			String str[] = new String[] { "分享到微博", "分享到腾讯微博", "取消" };
			dialog.setItemNames(str);
			dialog.setOnClickDialogListener(new OnClickDialogListener() {
				@Override
				public void onClick(int index) {
					switch (index) {
					case 0:
						break;
					case 1:
						break;
					case 2:// 取消键
						break;
					default:
						break;
					}
				}
			});
			dialog.show();
			break;
		case R.id.contack_service:
			// 弹出对话框
			serviceDialog = new android.app.AlertDialog.Builder(getActivity());
			serviceDialog.setTitle("提示").setMessage("确定要打电话给客服？")
					.setPositiveButton("确定", this).setNegativeButton("否", this)
					.show();
			break;
		case R.id.setting:
			startActivity(new Intent(getActivity(), SettingActivity.class));
			break;
		default:
			break;
		}

	}

	/**
	 * 响应对话框按钮
	 */
	@Override
	public void onClick(DialogInterface dialog, int which) {
		if (dialog.equals(serviceDialog)) {
			if (which == dialog.BUTTON_POSITIVE) {
				// 用intent启动拨打电话
				String number = "686063";
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ number));
				startActivity(intent);
			} else {

			}
		}
	}

	@Override
	public void onStop() {
		SunflowerApplication.mPicassoInstance.cancelRequest(avatarImageView);
		super.onStop();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		SunflowerApplication.mPicassoInstance.cancelRequest(avatarImageView);
	}
}
